package com.jpress.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

@Entity
@Table(name="usuario")
@NamedQueries({
	@NamedQuery(name="Usuario.countUsuarios", query="SELECT COUNT(u) FROM Usuario u"),
	@NamedQuery(name="Usuario.findByEmailAndSenha", query="SELECT u FROM Usuario u WHERE u.email = :email AND u.senha = :senha"),
	@NamedQuery(name="Usuario.findByNome", query="SELECT u FROM Usuario u WHERE u.nome like :nome ORDER BY u.nome ASC"),
	@NamedQuery(name="Usuario.findByEmail", query="SELECT u FROM Usuario u WHERE u.email = :email"),
	@NamedQuery(name="Usuario.findAll", query="SELECT u FROM Usuario u"),
	@NamedQuery(name="Usuario.pesquisar", query="SELECT u FROM Usuario u WHERE u.nome LIKE :nome AND u.email LIKE :email"),
	@NamedQuery(name="Usuario.ultimosCadastrados", query="SELECT u FROM Usuario u ORDER BY u.dataCadastro")
	
})
public class Usuario implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="codigo")
	private int codigo;
	
	@Column(name="nome", nullable=false, length=100)
	private String nome;
	
	@Column(name="email", unique=true, nullable=false, length=100)
	private String email;
	
	@Column(name="senha", nullable=false, length=32)
	private String senha;
	
	@Column(name="avatar", nullable=true, length=500)
	private String avatar;
	
	@Column(name="sexo", nullable=true, length=1) //M ou F
	private String sexo;
	
	@Column(name="ativo")
	private boolean ativo;
	
	@Column(name="admin")
	private boolean admin;
	
	@Column(name="biografia", nullable=true, length=255)
	private String biografia;
	
	@Column(name="local", nullable=true, length=200)
	private String local;
	
	@Column(name="dataNascimento", nullable=false)
	@Temporal(TemporalType.DATE)
	private Date dataNascimento;
	
	@Column(name="dataCadastro", nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date dataCadastro;

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="autor")
	private List<Comentario> comentarios;
	
	@OneToOne(cascade=CascadeType.PERSIST, fetch=FetchType.EAGER, mappedBy="dono")
    private Blog blog;
	
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="autor")
    private List<Post> posts;	
	
	public Usuario(){
		this.comentarios = new ArrayList<Comentario>();
		this.posts = new ArrayList<Post>();
	}

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getSexo() {
		return sexo;
	}

	public void setSexo(String sexo) {
		this.sexo = sexo;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public String getBiografia() {
		return biografia;
	}

	public void setBiografia(String biografia) {
		this.biografia = biografia;
	}

	public String getLocal() {
		return local;
	}

	public void setLocal(String local) {
		this.local = local;
	}

	public Date getDataNascimento() {
		return dataNascimento;
	}

	public void setDataNascimento(Date dataNascimento) {
		this.dataNascimento = dataNascimento;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}
	

	public void addComentario(Comentario comentario){
		this.comentarios.add(comentario);
		comentario.setAutor(this);
	}

	public List<Comentario> getComentarios() {
		return comentarios;
	}

	public void setComentarios(List<Comentario> comentarios) {
		this.comentarios = comentarios;
	}
	
	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	@Override
	public boolean equals(Object object){
		return EqualsBuilder.reflectionEquals(this, object);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public String toString() {
		return "Usuario [codigo=" + codigo + ", nome=" + nome + ", email="
				+ email + ", senha=" + senha + ", avatar=" + avatar + ", sexo="
				+ sexo + ", ativo=" + ativo + ", admin=" + admin
				+ ", biografia=" + biografia + ", local=" + local
				+ ", dataNascimento=" + dataNascimento + ", dataCadastro="
				+ dataCadastro + "]";
	}
}
